const { readFiles, joinAliPath } = require('../utils/utils')
const path = require("path");
const OSS = require("ali-oss");
const chalk = require('chalk')

module.exports = async function ({ config, localPath, ossName }) {
    const ossConfig = config?config:this.getOssConfig()
    const client = new OSS(ossConfig);
    const filePaths = readFiles(path.resolve(localPath));
    const requestArray = filePaths.map((item) => {
        const ossPath = path.relative(localPath, item);
        return client.put(joinAliPath(ossName, ossPath), item);
    });
    try {
        await Promise.all(requestArray);
        console.log(chalk.green("上传完成"))
    } catch (error) {
        console.log(error)
    }
}